package com.wms.wms.service;

import com.wms.wms.entity.Existing;
import com.wms.wms.entity.OutStock;
import com.wms.wms.entity.Product;
import com.wms.wms.entity.Type;
import com.wms.wms.mapper.ExistingMapper;
import com.wms.wms.mapper.product.ProductMapper;
import com.wms.wms.mapper.product.TypeMapper;
import com.wms.wms.mapper.product.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

@Service
public class ExistingService {
    @Autowired
    ExistingMapper existingMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    TypeMapper typeMapper;

    //通过id查询库存
    public int inventoryByParam(String id)
    {
        if(id.compareTo("000000") != 0) {
            return existingMapper.queryInventoryByParam(id);
        }
        else
            //总的库存
            return existingMapper.queryTotalInventory();
    }
    //通过typeid查询库存
    public int inventoryByType(String typeId)
    {
        List<Product> idList = productMapper.queryIdByType(typeId);
        int count = 0;
        for(Product id:idList){
            count += existingMapper.queryInventoryByParam(id.getId());
        }
        return count;
    }
    //通过id查询过期产品的数量
    public int outOfDateByParam(String id) throws ParseException {
        if(id.compareTo("000000") !=0 )
        {
            int guarantee_time = productMapper.queryGuaranteeTime(id);
            Date time = getDate(guarantee_time);
            return existingMapper.queryOutOfDateByParam(id, time);
        }
        else
        {
            //总的过期数量
            List<Type> typeList = typeMapper.queryType();
            int count = 0;
            for(Type type_id:typeList)
            {
                count += outOfDateByType(type_id.getId());
            }
            return count;
        }
    }

    //通过typeid查询过期产品数量
    public int outOfDateByType(String typeId) throws ParseException {
        List<Product> idList = productMapper.queryIdByType(typeId);
        int count = 0;
        for(Product id:idList){

            // int guarantee_time = productMapper.queryGuaranteeTime(id.getId());
            // Date time = getDate(guarantee_time);
            // count += existingMapper.queryOutOfDateByParam(id.getId(), time);

        }
        return count;
    }

    //根据保质期和当前日期计算得到生产日期
    public Date getDate(int guarantee_time) throws ParseException {
        Calendar calendar= Calendar.getInstance();
        int y=calendar.get(Calendar.YEAR);
        int m=calendar.get(Calendar.MONTH);
        int d=calendar.get(Calendar.DATE);
        int guarantee = guarantee_time;
        if(d <= guarantee)
        {
            guarantee -= d;
            d = 1;
        }
        else
        {
            d = d -guarantee;
            guarantee = 0;
        }
        while(guarantee > 0)
        {
            m--;
            if(m == 1||m == 3 ||m == 5||m == 7||m == 8||m == 10||m == 12)
            {
                if(guarantee >= 31)
                {
                    guarantee -= 31;
                }
                else
                {
                    d = 31 - guarantee;
                    guarantee = 0;
                }
            }
            else if( m == 2)
            {
                if((y % 400 == 0 || (y % 4 == 0)&&(y % 100 !=0)) )
                {
                    if(guarantee >= 29)
                    {
                        guarantee -= 29;
                    }
                    else
                    {
                        d = 29 - guarantee;
                        guarantee = 0;
                    }
                }
                else
                {
                    if(guarantee >= 28)
                    {
                        guarantee -= 28;
                    }
                    else
                    {
                        d = 28 - guarantee;
                        guarantee = 0;
                    }
                }

            }
            else if(m == 3||m == 5||m == 9||m == 11)
            {
                if(guarantee >= 30)
                {
                    guarantee -= 30;
                }
                else
                {
                    d = 30 - guarantee;
                    guarantee = 0;
                }
            }
            else if(m == 0)
            {
                y--;
                m = 12;
            }
        }
        String year = String.valueOf(y);
        String month = String.valueOf(m);
        String day = String.valueOf(d);
        String time = year+"-"+month+"-"+day;
        Date b = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(time).getTime());
        return b;
    }


    public int outDateCountByIdAndDate(String id,Date date) throws ParseException {
        int count = 0;
        if(id.compareTo("000000") != 0){
            for(Existing e:outDateByIdAndDate(id,date)){
                count += e.getNumber();
            }
        }
        else{
            for(Existing e:outDateByDate(date)){
                count += e.getNumber();
            }
        }
        return count;
    }
    public int outDateCountByTypeAndDate(String id,Date date) throws ParseException {
        int count = 0;
        for(Existing e:outDateByTypeAndDate(id,date)){
            count += e.getNumber();
        }
        return count;
    }
    public ArrayList<Existing> queryByIdAndDate(String id, Date bDate, Date eDate){
        return existingMapper.queryByIdAndDate(id,bDate,eDate);
    }

    public ArrayList<Existing> queryByDate(Date bDate, Date eDate){
        return existingMapper.queryByDate(bDate,eDate);
    }

    public ArrayList<Existing> queryByTypeAndDate(String id, Date bDate, Date eDate){
        List<Product> idList = productMapper.queryIdByType(id);
        ArrayList<Existing> outStockList = new ArrayList<>();
        for(Product i:idList){
            outStockList.addAll(existingMapper.queryByIdAndDate(i.getId(),bDate,eDate));
        }
        return outStockList;
    }

    public ArrayList<Existing> outDateByIdAndDate(String id, Date b) throws ParseException {
        String d = "2000-01-01";
        Date date = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(d).getTime());
        ArrayList<Existing> list = existingMapper.queryByDate(date,b );
        ArrayList<Existing> result = new ArrayList<>();
        for(Existing e:list){
            int guar = productMapper.queryGuaranteeTime(e.getId());
            if(ifOutOfDate(e.getProduceDate(),b,guar))
                result.add(e);
        }
        return result;
    }

    private boolean ifOutOfDate(Date produceDate, Date date, int guar){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(produceDate);
        calendar.add(Calendar.DATE, guar);

        return new Date(calendar.getTime().getTime()).after(date);
    }
    public ArrayList<Existing> outDateByDate(Date b) throws ParseException {
        String d = "2000-01-01";
        Date date = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(d).getTime());
        ArrayList<Existing> list = existingMapper.queryByDate(date,b );
        ArrayList<Existing> result = new ArrayList<>();
        for(Existing e:list){
            int guar = productMapper.queryGuaranteeTime(e.getId());
            if(!ifOutOfDate(e.getProduceDate(),b,guar))
                result.add(e);
        }
        return result;
    }

    public ArrayList<Existing> outDateByTypeAndDate(String typeId, Date b) throws ParseException {
        String d = "2000-01-01";
        Date date = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(d).getTime());
        ArrayList<Existing> list = queryByTypeAndDate(typeId, date,b );
        ArrayList<Existing> result = new ArrayList<>();
        for(Existing e:list){
            int guar = productMapper.queryGuaranteeTime(e.getId());
            if(!ifOutOfDate(e.getProduceDate(),b,guar))
                result.add(e);
        }
        return result;
    }

    public int existCountByParam(String id, Date e) throws ParseException {
        String d = "2000-01-01";
        Date b = new Date(new SimpleDateFormat("yyyy-MM-dd").parse(d).getTime());
        int count = 0;
        for(Existing ex:queryByIdAndDate(id,b,e)){
            count+=ex.getNumber();
        }
        return count;
    }

    public int existCountByType(String typeId, Date e) throws ParseException {
        int count = 0;
        List<Product> idList = productMapper.queryIdByType(typeId);
        ArrayList<Existing> outStockList = new ArrayList<>();
        for(Product i:idList){
            count += existCountByParam(i.getId(),e);
        }
        return  count;
    }
}
